/**
 * 判断是否为空
 */
function validatenull(val) {
	if (typeof val == 'boolean') {
		return false;
	}
	if (typeof val == 'number') {
		return false;
	}
	if (val instanceof Array) {
		if (val.length == 0) return true;
	} else if (val instanceof Object) {
		if (JSON.stringify(val) === '{}') return true;
	} else {
		if (val == 'null' || val == null || val == 'undefined' || val == undefined || val == '') return true;
		return false;
	}
	return false;
}
const keyName = "wxapp" + '-';
/**
 * 存储localStorage
 */
export const setStore = (params = {}) => {
	let {
		name,
		content
	} = params;
	name = keyName + name
	let obj = {
		dataType: typeof(content),
		content: content,
		datetime: new Date().getTime()
	}
	uni.setStorageSync(name, JSON.stringify(obj));
}
/**
 * 获取localStorage
 */

export const getStore = (params = {}) => {
	let {
		name,
		debug
	} = params;
	name = keyName + name
	let obj = {},
		content;
	obj = uni.getStorageSync(name);
	// obj = window.sessionStorage.getItem(name);
	if (validatenull(obj)) obj = uni.getStorage({
		key: name,
		success: function(res) {}
	});
	if (validatenull(obj)) return;
	try {
		obj = JSON.parse(obj);
	} catch {
		return obj;
	}
	if (debug) {
		return obj;
	}
	if (obj.dataType == 'string') {
		content = obj.content;
	} else if (obj.dataType == 'number') {
		content = Number(obj.content);
	} else if (obj.dataType == 'boolean') {
		content = Boolean(obj.content);
	} else if (obj.dataType == 'object') {
		content = obj.content;
	}
	return content;
}

// /**
//  * 清空全部localStorage
//  */
export const clearStore = (params = {}) => {
	uni.clearStorageSync();
}
